package com.shisfish.library.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.shisfish.library.Impl.BookMessageImpl;
import com.shisfish.library.Model.BookMessage;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;

@Path("bookMessage")
public class BookMessageController extends GlobalVariable{

	BookMessageImpl bookMessageImpl = new BookMessageImpl();
	
	@POST
	@Path("insertOne")
	@Produces("application/json; charset=utf-8")
	public String insertOne(@FormParam("isbn") String isbn, @FormParam("total") int total, 
			@FormParam("surplus") int surplus, @FormParam("surplus_show") int surplus_show,
			@FormParam("isBespeak") int isBespeak, @FormParam("number") int number){
		Map<String, Object> globalMap = new HashMap<String, Object>();
		BookMessage bookMessage = new BookMessage(isbn, total, surplus, surplus_show, isBespeak, number);
		
		int flag = bookMessageImpl.insertOne(bookMessage);
		
		if (flag == 1) {
			globalMap.put("message", ADD_SUCCESS);
		}
		else {
			globalMap.put("message", ADD_ERROR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectAll")
	@Produces("application/json; charset=utf-8")
	public String selectAll(){
		Map<String, Object> globalMap = new HashMap<String, Object>();
		List<BookMessage> bookMessages = bookMessageImpl.selectAll();
		
		if (bookMessages.size() > 0) {
			globalMap.put("list", bookMessages);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}

}
